package duckHunter.ui;


import java.io.*;
import javax.microedition.lcdui.*;

import duckHunter.app.DuckHunter;

/**
 * menueCanvas class extends Canvas 
 * it represents the principle page of the application
 * it gives the user navigation choices
 * 
 * @author AYARI
 *
 */

public class menuCanvas extends Canvas 
{
	//strings to be added to the screen
	String [] myStrings  = {"Start","Help","High Score"};

	//integer used for navigation purposes
	private int selectedIndex = 0;

	//an instance of the midlet class
	public DuckHunter parent;

	//for user interaction
	public static int iKEY_UP = -1;
	public static int iKEY_DOWN = -2;
	public static int iKEY_LEFT = -3;
	public static int iKEY_RIGHT = -4;
	public static int iKEY_FIRE = -5;
	public static int iKEY_SOFT1 = -6;
	public static int iKEY_SOFT2 = -7;

	//constructor which takes SaveTheWorld (MIDlet) instance as input
	public menuCanvas(DuckHunter midlet)
	{   
		this.setCommandListener(midlet);
		parent = midlet; 

	}

	//paint the graphics of the screen
	protected void paint(Graphics g) {

		drawBackground(g);
		drawImageButtons(g);

	}

	//draw the background which is a white background
	void drawBackground(Graphics g){
		g.setColor(255,255,255);
		g.fillRect(0, 0, getWidth(), getHeight());
	}

	//draw the strings and rectangles according to the index specifies
	// (like buttons created)
	void drawImageButtons(Graphics g)
	{
		//set the color of the rectangles
		//create rectangles according to selectedIndex
		g.setColor(0,190,0);
		if(selectedIndex == 0)
			g.fillRect(80,188,100,20);
		else if(selectedIndex == 1)
			g.fillRect(80,208,100,20);
		else if(selectedIndex == 2)
			g.fillRect(80,228,100,20);
		
		//add an image
		Image save;
		try {
			save = Image.createImage("/img/game.png");
			g.drawImage(save, 60,20 , 0);
		} catch (IOException e) {
			e.printStackTrace();
		}
		//set the color and the font of the strings
		g.setColor(0,0,0); 
		g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM));

		//draw strings 
		g.drawString(myStrings[0], 110, 190, Graphics.LEFT | Graphics.TOP);
		g.drawString(myStrings[1], 110, 210, Graphics.LEFT | Graphics.TOP);
		g.drawString(myStrings[2], 90, 230, Graphics.LEFT | Graphics.TOP);
	}

	//listens what key is pressed and change the value of the index
	public void keyPressed(int key)
	{

		if(key == iKEY_DOWN)
		{
			if(selectedIndex < 3)
				selectedIndex++;
			else if(selectedIndex == 3)
				selectedIndex = 0;
		}
		else if(key == iKEY_UP)
		{
			if(selectedIndex > 0)
				selectedIndex--;
			else if(selectedIndex == 0)
				selectedIndex = 3;
		}
		else if(key == iKEY_SOFT1 || key == iKEY_FIRE)   
		{
			keypressFIRE();
		}
		else if(key == iKEY_SOFT2)
			parent.destroyApp(true);


		repaint();
	}
	//If the fire key is pressed depending on the selected index 
	//it calls a method of the parent
	public void keypressFIRE()
	{
		if(selectedIndex == 0)
			parent.startGame();
		else if(selectedIndex == 1)
			parent.startHelp();
		else if(selectedIndex == 2)
			parent.startHighScore();
		
	}
}